package com.market.plus.ocm.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.market.plus.core.util.MarketUtil;
import com.ocm.core.constant.OCMConstant;
import com.ocm.core.domain.AccessMask;
import com.ocm.core.dto.GroupDto;
import com.ocm.core.dto.SecurityDto;
import com.ocm.core.dto.UserInfoDto;
import com.ocm.core.service.ContentEngineService;
import com.opm.security.core.util.UserUtil;

@Service("marketOCMSecurityService")
public class MarketOCMSecurityServiceImpl implements MarketOCMSecurityService {

   private ContentEngineService contentEngineService;

   @Autowired
   public MarketOCMSecurityServiceImpl(@Qualifier("localContentEngineService") ContentEngineService contentEngineService) {
      this.contentEngineService = contentEngineService;
   }

   public List<SecurityDto> getAvatar(long userCreateId) {
      List<SecurityDto> securityDtos = new ArrayList<SecurityDto>();
      securityDtos.add(getCreator(userCreateId, AccessMask.FULLPERMISSION.getAccessMask()));
      GroupDto groupDto = this.contentEngineService.loadByGroupname(MarketOCMConstant.ROLE_SOCICAL_PUBLIC);
      securityDtos.add(getGroup(groupDto.getId(), AccessMask.VIEWPROPERTIES.getAccessMask()));
      return securityDtos;
   }

   public List<SecurityDto> getAlbum(long userCreateId) {
      List<SecurityDto> securityDtos = new ArrayList<SecurityDto>();
      securityDtos.add(getCreator(userCreateId, AccessMask.FULLPERMISSION.getAccessMask()));
      GroupDto groupDto = this.contentEngineService.loadByGroupname(MarketOCMConstant.ROLE_SOCICAL_PUBLIC);
      securityDtos.add(getGroup(groupDto.getId(), AccessMask.VIEWPROPERTIES.getAccessMask()));
      return securityDtos;
   }

   public List<SecurityDto> getParticipant(long userCreateId) {
      List<SecurityDto> securityDtos = new ArrayList<SecurityDto>();
      securityDtos.add(getCreator(userCreateId, AccessMask.FULLPERMISSION.getAccessMask()));
      GroupDto groupDto = this.contentEngineService.loadByGroupname(MarketOCMConstant.ROLE_SOCICAL_PUBLIC);
      securityDtos.add(getAnonymous(groupDto.getId(), AccessMask.LINKPROPERTIES.getAccessMask()));
      return securityDtos;
   }

   public List<SecurityDto> getImage() {
      UserInfoDto userDetails = (UserInfoDto) MarketUtil.getCustomUserDetails().getExtUserDetails();
      List<SecurityDto> securityDtos = new ArrayList<SecurityDto>();
      securityDtos.add(getCreator(userDetails.getId(), AccessMask.FULLPERMISSION.getAccessMask()));
      GroupDto groupDto = this.contentEngineService.loadByGroupname(MarketOCMConstant.ROLE_SOCICAL_PUBLIC);
      securityDtos.add(getGroup(groupDto.getId(), AccessMask.LINKPROPERTIES.getAccessMask()));
      return securityDtos;
   }

   public List<SecurityDto> getAlbum() {
      UserInfoDto userDetails = (UserInfoDto) UserUtil.getAuthenticated();
      List<SecurityDto> securityDtos = new ArrayList<SecurityDto>();
      securityDtos.add(getCreator(userDetails.getId(), AccessMask.FULLPERMISSION.getAccessMask()));
      GroupDto groupDto = this.contentEngineService.loadByGroupname(MarketOCMConstant.ROLE_SOCICAL_PUBLIC);
      securityDtos.add(getGroup(groupDto.getId(), AccessMask.VIEWPROPERTIES.getAccessMask()));
      return securityDtos;
   }

   public List<SecurityDto> getNew(long[] myFriendIds) {
      UserInfoDto userDetails = (UserInfoDto) MarketUtil.getCustomUserDetails().getExtUserDetails();
      List<SecurityDto> securityDtos = new ArrayList<SecurityDto>();
      securityDtos.add(getCreator(userDetails.getId(), AccessMask.FULLPERMISSION.getAccessMask()));
      if (myFriendIds != null){
         for (long myFriendId : myFriendIds){
            securityDtos.add(getUser(myFriendId, AccessMask.LINKPROPERTIES.getAccessMask()));
         }
      }
      return securityDtos;
   }

   public List<SecurityDto> getTribe(long[] myFriendIds, boolean hasPublic) {
      UserInfoDto userDetails = (UserInfoDto) MarketUtil.getCustomUserDetails().getExtUserDetails();
      List<SecurityDto> securityDtos = new ArrayList<SecurityDto>();
      securityDtos.add(getCreator(userDetails.getId(), AccessMask.FULLPERMISSION.getAccessMask()));
      GroupDto groupDto = this.contentEngineService.loadByGroupname(OCMConstant.SEC_ANONYMOUS_GROUP);
      securityDtos.add(getGroup(groupDto.getId(), AccessMask.LINKPROPERTIES.getAccessMask()));
      return securityDtos;
   }

   public List<SecurityDto> getComment(UserInfoDto userInfoDto) {
      GroupDto groupDto = this.contentEngineService.loadByGroupname(OCMConstant.SEC_ANONYMOUS_GROUP);
      UserInfoDto userDetails = (UserInfoDto) MarketUtil.getCustomUserDetails().getExtUserDetails();
      List<SecurityDto> securityDtos = new ArrayList<SecurityDto>();
      Long currentLogin = userDetails.getId();
      securityDtos.add(getCreator(currentLogin, AccessMask.FULLPERMISSION.getAccessMask()));
      securityDtos.add(getAnonymous(groupDto.getId(), AccessMask.VIEWPROPERTIES.getAccessMask()));
      if (currentLogin.longValue() != userInfoDto.getId().longValue())
         securityDtos.add(getUser(userInfoDto.getId(), AccessMask.FULLPERMISSION.getAccessMask()));
      return securityDtos;
   }

   private SecurityDto getCreator(long userId, int accessMask){
      SecurityDto securityDto = getSecurityDto(userId, accessMask, true, false, false, false);
      return securityDto;
   }

   private SecurityDto getAnonymous(long userId, int accessMask){
      SecurityDto securityDto = getSecurityDto(userId, accessMask, false, false, false, true);
      return securityDto;
   }

   private SecurityDto getUser(long userId, int accessMask){
      SecurityDto securityDto = getSecurityDto(userId, accessMask, false, false, true, false);
      return securityDto;
   }

   private SecurityDto getGroup(long userId, int accessMask){
      SecurityDto securityDto = getSecurityDto(userId, accessMask, false, true, false, false);
      return securityDto;
   }

   private SecurityDto getSecurityDto(long userCreateId, int accessMask, boolean hasCreator, boolean hasGroup, boolean hasUser, boolean hasAnonymous){
      SecurityDto securityDto = new SecurityDto();
      securityDto.setAccessMask(accessMask);
      securityDto.setHasCreator(hasCreator);
      securityDto.setUserId(userCreateId);
      securityDto.setHasAnonymous(hasAnonymous);
      securityDto.setHasGroup(hasGroup);
      securityDto.setHasUser(hasUser);
      return securityDto;
   }
}
